package com.huiquan.icd.service;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.*;
import com.huiquan.icd.dao.IcdDiseaseExternalDao;
import com.huiquan.icd.dao.IcdDiseaseHospitalDao;
import com.huiquan.icd.domain.IcdDiseaseExternal;
import com.huiquan.synonymy.dao.SynonymyDao;
import com.huiquan.synonymy.dao.SynonymyWordDao;
import com.huiquan.synonymy.domain.Synonymy;
import com.huiquan.synonymy.domain.SynonymyWord;
import com.huiquan.synonymy.service.SynonymyService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

@Service
public class IcdDiseaseExternalService extends BaseService {
	@Autowired
	private IcdDiseaseExternalDao icdDiseaseExternalDao;
	@Autowired
	private IcdDiseaseHospitalDao icdDiseaseHospitalDao;
	@Autowired
	private SynonymyWordDao synonymyWordDao;
	@Autowired
	private SynonymyService synonymyService;
	@Autowired
	private SynonymyDao synonymyDao;

	public ModelAndView list(Integer start, String codeSearch, String exactSearchFlag, String countPerPageStr,
			String checkFlag,String hospitalId) {
		boolean exactFlag = exactSearchFlag != null && exactSearchFlag.equals("1");

		Map<String, Object> param = new HashMap<>();
		if (codeSearch != null && !codeSearch.isEmpty()) {
			param.put("diseaseName", exactFlag ? codeSearch : "%" + codeSearch + "%");
		}
		if ("1".equals(checkFlag)) {
			param.put("orderStr", "a.show_disease ");
		} else {
			param.put("orderStr", "a.disease_name ");
		}
		param.put("checkFlag", checkFlag);
		param.put("hospitalId", hospitalId);

		int totalSize = icdDiseaseExternalDao.retrieveSize(param);
		Map<String, Integer> pageNo = new HashMap<>();
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, start + "", countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, start + "");
		}
		int endPage = pageNo.get("endPage");
		start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<IcdDiseaseExternal> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			list = icdDiseaseExternalDao.retrieveList(param);
		}
		for (IcdDiseaseExternal value : list) {
			value.setExample(value.getExample().replace((char) 1 + "", "</br>"));
		}
		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("codeSearch", "疾病名");

		Map<String, String> searchMap = new HashMap<>();
		searchMap.put("codeSearch", codeSearch);
		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, searchMap);
		map.put("showSearch", showSearch);
		map.put("showSearchValue", searchMap);
		map.put("exactSearchFlag", exactSearchFlag);
		return new ModelAndView("icd/index", map);
	}

	public ReturnData checkExternalDisease(String id, String disease) {
		// 判断是否有同义词
		if (StringUtils.isNotBlank(disease)) {
			IcdDiseaseExternal external = icdDiseaseExternalDao.retrieveObjectById(Long.parseLong(id));
			// 如果相同则直接记录
			if (!StringUtils.equalsIgnoreCase(external.getDiseaseName(), disease)) {
				SynonymyWord syw = synonymyWordDao.retrieveDiSynonymyByName(disease);
				if (syw == null) {
					// 判断是否在疾病维表中
					Map<String, Object> param = new HashMap<>();
					param.put("diseaseName", disease);
					List<String> value = icdDiseaseExternalDao.getAllDiseaseNameWithOutSynonymy(param);
					if (value == null || value.size() == 0) {
						ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
						rc.setDesc("该词不在同义词和疾病维表中");
						return ReturnUtil.fail(rc);
					}
				}
			}
			return ReturnUtil.success();
		} else {
			ReturnCode rc = ReturnCode.PROPERTY_IS_EMPTY;
			return ReturnUtil.fail(rc);
		}
	}

	public ReturnData changeDisable(String id, String disable) {
		IcdDiseaseExternal external = icdDiseaseExternalDao.retrieveObjectById(Long.parseLong(id));
		icdDiseaseExternalDao.updateDisable(external.getShowDisease(), disable);
		return ReturnUtil.success();
	}

	public ReturnData changeExternalDisease(String id, String disease, User user) throws Exception {
		IcdDiseaseExternal external = icdDiseaseExternalDao.retrieveObjectById(Long.parseLong(id));
		external.setShowDisease(disease);
		icdDiseaseExternalDao.update(external);
		// 如果是已校验的要进行同义词变换
		if ("1".equals(external.getCheckFlag())) {
			changeSynonymy(id, user);
		}
		return ReturnUtil.success();
	}
	public ReturnData changeExternalCategory(String id, String category, User user) throws Exception {
		IcdDiseaseExternal external = icdDiseaseExternalDao.retrieveObjectById(Long.parseLong(id));
		if(StringUtils.isNotBlank(external.getShowDisease())){
			//判断showDisease是否是疾病维表的疾病名
			Map<String, Object> param = new HashMap<>();
			param.put("diseaseName", external.getShowDisease());
			List<Map<String, Object>> diseases = icdDiseaseHospitalDao.getDiseaseDisableCategory(param);
			String oldCategory = "";
			if(diseases != null && diseases.size()>0){
				oldCategory = (String) diseases.get(0).get("category");
				if(StringUtils.equals(category, oldCategory)){
					//更新
					icdDiseaseExternalDao.updateCategory(external.getShowDisease(), oldCategory);
					return ReturnUtil.success();
				}else{
					icdDiseaseExternalDao.updateCategory(external.getShowDisease(), oldCategory);
					ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
					rc.setDesc("更新的一级与疾病维表不一致,现更新成疾病维表的一级");
					return ReturnUtil.fail(rc);
				}
			}else{
				//不在疾病维表中,可以在外部疾病中
				List<String> allExternalDisease = icdDiseaseExternalDao.getAllExternalDisease();
				if(allExternalDisease.contains(category)){
					icdDiseaseExternalDao.updateCategory(external.getShowDisease(), category);
				}else{
					ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
					rc.setDesc("该一级不在疾病维表中,也不是有效的外部疾病");
					return ReturnUtil.fail(rc);
				}
			}
			
			
		}else{
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc("该疾病名不能为空");
			return ReturnUtil.fail(rc);
		}
		return ReturnUtil.success();
	}
	public ReturnData submit(String id, User user) throws Exception {
		String[] ids = id.split(",");
		for (String externalId : ids) {
			changeSynonymy(externalId, user);
			updateCheckFlag(externalId);
		}

		return ReturnUtil.success();
	}

	private void updateCheckFlag(String id) {
		IcdDiseaseExternal external = icdDiseaseExternalDao.retrieveObjectById(Long.parseLong(id));
		if (StringUtils.isNotBlank(external.getShowDisease()) && StringUtils.isNotBlank(external.getDisable())) {
			// 提交checkFlag
			icdDiseaseExternalDao.updateCheckFlag(Long.parseLong(id));
		}
	}

	private ReturnData changeSynonymy(String id, User user) throws Exception {
		IcdDiseaseExternal external = icdDiseaseExternalDao.retrieveObjectById(Long.parseLong(id));
		String disease = external.getShowDisease();
		if (StringUtils.isNotBlank(disease) && StringUtils.isNotBlank(external.getDisable())) {
			// 如果相同直接修改
			if (StringUtils.equalsIgnoreCase(external.getDiseaseName(), disease)) {
				return ReturnUtil.success();
			}
			// 添加同义词
			List<String> words = synonymyWordDao.retrieveWordListByWord(disease, 1, "di");
			ReturnData rtn = null;
			// 存在同义词
			if (words != null && words.size() > 0) {
				SynonymyWord syw = synonymyWordDao.retrieveDiSynonymyByName(disease);
				Synonymy sy = synonymyDao.retrieveObjectById(syw.getSynonymyId());
				if (!words.contains(external.getDiseaseName())) {
					words.add(external.getDiseaseName());
					rtn = synonymyService.edit(1, syw.getSynonymyId() + "", "di",
							(String[]) words.toArray(new String[words.size()]), user, sy.getStd(), "", false);
					if (!StringUtils.equals(rtn.getReturnCode().getCode(), "0")) {
						throw new RuntimeException(rtn.getReturnCode().getDesc() + ":" + disease);
					}
				}
			} else {
				// 不存在同义词
				words = new ArrayList<>();
				words.add(external.getDiseaseName());
				words.add(disease);
				rtn = synonymyService.edit(1, "", "di", (String[]) words.toArray(new String[words.size()]), user,
						disease, "", false);
				if (StringUtils.equals(rtn.getReturnCode().getCode(), "0")) {
					external.setShowDisease(disease);
					icdDiseaseExternalDao.update(external);
				}
				if (!StringUtils.equals(rtn.getReturnCode().getCode(), "0")) {
					throw new RuntimeException(rtn.getReturnCode().getDesc() + ":" + disease);
				}
			}
			return rtn;
		}

		return ReturnUtil.success();
	}

	public ReturnData initExternalData() {

		// 先根据疾病表初始化与疾病名相同的记录,再根据同义词表初始化不相同的记录
		icdDiseaseExternalDao.initShowDisease();

		// 查询show_disease为空的记录进行例子的生成
		Map<String, Object> param = new HashMap<>();
		List<IcdDiseaseExternal> diseaseValues = icdDiseaseExternalDao.retrieveList(param);
		List<IcdDiseaseExternal> insertList = new ArrayList<>();
		if (diseaseValues != null && diseaseValues.size() > 0) {
			// 获取所有的疾病名(包括疾病同义词)
			List<String> allDisease = icdDiseaseExternalDao.getAllDiseaseName();
			// 获取同义词对应map
			Map<String, String> synonymyMap = getSynonymyMap();
			for (IcdDiseaseExternal diseaseValue : diseaseValues) {
				// 记录匹配数量对应词的Map
				TreeMap<Integer, List<String>> cntAndWord = new TreeMap<>();
				// 每一个疾病
				matchWord(diseaseValue.getDiseaseName(), allDisease, cntAndWord);
				// 循环结束.匹配数量最高的list.取5个标准词
				diseaseValue.setExample(getExampleStr(cntAndWord, synonymyMap));
				insertList.add(diseaseValue);
			}

			// 删除为空的记录重新批量insert
			icdDiseaseExternalDao.initBeforeBatchInsert();
			icdDiseaseExternalDao.batchInsert(insertList);
			icdDiseaseExternalDao.updateExample();

		}

		return ReturnUtil.success();
	}

	public ReturnData initExternalDisease() {

		// 先根据疾病表初始化与疾病名相同的记录,再根据同义词表初始化不相同的记录
		icdDiseaseExternalDao.initShowDisease();

		return ReturnUtil.success();
	}
	
	private void matchWord(String diseaseName, List<String> allDisease, TreeMap<Integer, List<String>> cntAndWord) {
		for (String disease : allDisease) {
			int tempCnt = 0;
			for (int i = 0; i < diseaseName.length(); i++) {
				// 匹配上一个记录
				if (disease.contains(diseaseName.charAt(i) + "")) {
					tempCnt++;
				}
			}
			if (cntAndWord.get(tempCnt) != null) {
				cntAndWord.get(tempCnt).add(disease);
			} else {
				List<String> words = new ArrayList<>();
				words.add(disease);
				cntAndWord.put(tempCnt, words);
			}
		}
	}

	@SuppressWarnings("rawtypes")
	private String getExampleStr(TreeMap<Integer, List<String>> cntAndWord, Map<String, String> synonymyMap) {
		if (cntAndWord != null && cntAndWord.size() > 0) {
			NavigableSet nset = cntAndWord.descendingKeySet();
			Set<String> example = new HashSet<>();
			Iterator it = nset.iterator();
			while (it.hasNext()) {
				List<String> wordList = cntAndWord.get(it.next());
				for (String word : wordList) {
					if (synonymyMap.get(word) != null) {
						example.add(synonymyMap.get(word));
					} else {
						example.add(word);
					}
					if (example.size() == 5) {
						break;
					}
				}
				if (example.size() == 5) {
					break;
				}
			}
			StringBuffer str = new StringBuffer();
			if (example != null && example.size() > 0) {
				Iterator<String> its = example.iterator();
				while (its.hasNext()) {
					String exa = its.next();
					str.append(exa).append((char) 1 + "");
				}
				return str.substring(0, str.length() - 1).toString();
			}
		}
		return "";
	}

	private Map<String, String> getSynonymyMap() {
		Map<String, String> synonymyMap = new HashMap<>();
		List<Map<String, String>> synonymyWords = synonymyWordDao.retrieveElementSynonymByProperty("di");
		for (Map<String, String> synonymyWord : synonymyWords) {
			synonymyMap.put(synonymyWord.get("word"), synonymyWord.get("std"));
		}
		return synonymyMap;
	}

	public void export(HttpServletResponse resp) throws Exception {
		// 先根据疾病表初始化与疾病名相同的记录,再根据同义词表初始化不相同的记录
		icdDiseaseExternalDao.initShowDisease();

		// 设置respons的头信息
		RespHeaderUtil.setXlsxResp(resp, "外部疾病");

		Workbook workbook = new XSSFWorkbook(); // 创建文档

		List<IcdDiseaseExternal> externalList = icdDiseaseExternalDao.retrievesExportList();

		Sheet sheet = workbook.createSheet("外部疾病"); // 创建Sheet
		// 循环写入数据
		Cell cell = null;
		for (int i = 0; i < externalList.size(); i++) {
			IcdDiseaseExternal external = externalList.get(i);
			Row row = sheet.createRow(i);
			cell = row.createCell(0);
			cell.setCellValue(external.getDiseaseName());
			cell = row.createCell(1);
			cell.setCellValue(StringUtils.isNotBlank(external.getShowDisease()) ? external.getShowDisease() : "");
			cell = row.createCell(2);
			cell.setCellValue(StringUtils.isNotBlank(external.getDisable()) ? external.getDisable() : "");
			cell = row.createCell(3);
			cell.setCellValue(StringUtils.isNotBlank(external.getCategoryName()) ? external.getCategoryName() : "");

		}

		OutputStream os = resp.getOutputStream();
		workbook.write(os);

		os.close();
		workbook.close();
	}

	public ReturnData externalImport(MultipartFile file, User user) throws IOException {

		// 校验数据
		Map<String, Map<String, String>> valueList = new HashMap<>();
		ReturnData rtn = checkImportData(file, user, valueList);

		// 更新数据
		dealData(valueList);
		return rtn;

	}

	private void dealData(Map<String, Map<String, String>> valueList) {
		List<IcdDiseaseExternal> externalList = icdDiseaseExternalDao.retrievesExportList();
		for (IcdDiseaseExternal external : externalList) {
			if (valueList.get(external.getDiseaseName()) != null) {
				Map<String, String> value = valueList.get(external.getDiseaseName());
				if (!StringUtils.equals(value.get("std"), external.getShowDisease())
						|| !StringUtils.equals(value.get("disable"), external.getDisable())
						|| !StringUtils.equals(value.get("category"), external.getCategoryName())) {
					icdDiseaseExternalDao.updateAll(value);
				}
			}
		}
	}

	private ReturnData checkImportData(MultipartFile file, User user, Map<String, Map<String, String>> valueMap)
			throws IOException {
		// 获取文件流，解析文件
		InputStream inputStream = file.getInputStream();

		@SuppressWarnings("resource")
		Workbook workbook = new XSSFWorkbook(inputStream);
		Sheet sheet = workbook.getSheetAt(0);
		// 获取同义词构建对象
		Map<String, Object> param = new HashMap<>();
		param.put("type", 1);
		param.put("property", "di");
		List<Synonymy> synonymyList = synonymyDao.retrieveList(param);
		// 同义词对象列表
		Map<String, String> synonymyMap = new HashMap<>();
		for (Synonymy synonymy : synonymyList) {
			List<String> words = CollectionUtil
					.getListByArray(synonymy.getWords().substring(1, synonymy.getWords().length()).split("\t"));
			for (String word : words) {
				synonymyMap.put(word, synonymy.getStd());
			}
		}
		// 疾病维表中的疾病名对应disable,和一级关系
		Map<String, String> diseaseAndDisable = new HashMap<>();
		Map<String, String> diseaseAndCategory = new HashMap<>();
		// 疾病维表中可以作为一级的
		List<String> categoryDisease = new ArrayList<>();
		// 疾病维表中的疾病
		List<String> diseaseAll = new ArrayList<>();
		List<Map<String, Object>> diseases = icdDiseaseHospitalDao.getDiseaseDisableCategory(new HashMap<String,Object>());
		for (Map<String, Object> disease : diseases) {
			diseaseAll.add(disease.get("disease_name") + "");
			diseaseAndDisable.put(disease.get("disease_name") + "", disease.get("disable") + "");
			diseaseAndCategory.put(disease.get("disease_name") + "", disease.get("category") + "");
			if ("0".equals(disease.get("disable") + "")) {
				categoryDisease.add(disease.get("disease_name") + "");
			}
		}
		StringBuffer errMsg = new StringBuffer();
		StringBuffer errMsg2 = new StringBuffer();
		StringBuffer errMsg3 = new StringBuffer();
		StringBuffer errMsg4 = new StringBuffer();
		StringBuffer errMsg5 = new StringBuffer();
		StringBuffer errMsg6 = new StringBuffer();
		StringBuffer errMsg7 = new StringBuffer();
		StringBuffer warn1 = new StringBuffer();
		StringBuffer warn2 = new StringBuffer();

		// 有效性是否唯一
		Map<String, String> disableMap = new HashMap<>();
		// 一级是否一致
		Map<String, String> categoryMap = new HashMap<>();
		// 不在疾病维表中的一级
		List<String> categoryList = new ArrayList<>();
		// 外部疾病可以作为一级的
		List<String> categoryDiseaseList = new ArrayList<>();

		// 处理每一行
		for (Row row : sheet) {
			String diseaseName = getCellNum(row, 0);
			String std = getCellNum(row, 1);
			String disable = getCellNum(row, 2);
			String category = getCellNum(row, 3);
			String upperDisease = StringUpperUtil.changeStr(diseaseName);
			String upperStd = StringUpperUtil.changeStr(std);
			if (!"0".equals(disable) && !"1".equals(disable) && !"2".equals(disable)) {
				if (errMsg4.length() == 0) {
					errMsg4.append("该疾病的disable填写有误：" + diseaseName + ";");
				} else {
					errMsg4.append(diseaseName + ";");
				}
			}
			if (StringUtils.isBlank(diseaseName) || StringUtils.isBlank(std) || StringUtils.isBlank(disable)
					|| (StringUtils.isBlank(category) && !"2".equals(disable))) {
				if (errMsg.length() == 0) {
					errMsg.append("该疾病的疾病名,disable,一级必须填写：" + diseaseName + ";");
				} else {
					errMsg.append(diseaseName + ";");
				}
			}

			if (synonymyMap.get(upperDisease) != null && synonymyMap.get(upperStd) != null
					&& !StringUtils.equalsIgnoreCase(synonymyMap.get(upperDisease),
							synonymyMap.get(upperStd))) {
				if (errMsg2.length() == 0) {
					errMsg2.append("该疾病的同义词出现冲突：" + diseaseName + ";");
				} else {
					errMsg2.append(diseaseName + ";");
				}
			}

			String temp = "";
			String addTemp = "";
			// 对应疾病名
			String tempStd = "";
			if (synonymyMap.get(upperDisease) != null && synonymyMap.get(upperStd) == null) {
				temp = diseaseName;
				addTemp = std;
			} else if (synonymyMap.get(upperDisease) == null
					&& synonymyMap.get(upperStd) != null) {
				temp = std;
				addTemp = diseaseName;
			}
			// 添加同义词
			ReturnData rtn = null;
			if (StringUtils.isNotBlank(temp)) {
				SynonymyWord syw = synonymyWordDao.retrieveDiSynonymyByName(temp);
				Synonymy sy = synonymyDao.retrieveObjectById(syw.getSynonymyId());
				sy.setWords(sy.getWords() + StringUpperUtil.changeStr(addTemp) + "\t");
				rtn = synonymyService.edit(1, syw.getSynonymyId() + "", "di",
						sy.getWords().substring(1, sy.getWords().length()).split("\t"), user, sy.getStd(), "", false);
				tempStd = sy.getStd();
				LOGGER.info("添加同义词:" + addTemp + ";");
			} else if (synonymyMap.get(upperDisease) == null
					&& synonymyMap.get(upperStd) == null) {
				if (StringUtils.equals(diseaseName, std)) {
					// 相同不需要添加同义词
					tempStd = diseaseName;
				} else {
					// 新增同义词
					List<String> words = new ArrayList<>();
					words.add(upperDisease);
					words.add(upperStd);
					String synonymyStd = "";
					// 如果在疾病维表中,使用疾病维表中的词做疾病名
					if (diseaseAll.contains(diseaseName)) {
						synonymyStd = diseaseName;
					} else {
						synonymyStd = std;
					}
					rtn = synonymyService.edit(1, "", "di", (String[]) words.toArray(new String[words.size()]), user,
							synonymyStd, "", false);
					LOGGER.info("新增同义词:" + words.toString() + ";");
					tempStd = synonymyStd;
				}
			} else {
				// 已经在同一组同义词中,不需要添加同义词
				tempStd = synonymyMap.get(upperDisease);
			}
			if (rtn != null && !StringUtils.equals(rtn.getReturnCode().getCode(), "0")) {
				errMsg3.append(rtn.getReturnCode().getDesc() + ":" + diseaseName + ";");
			}

			// 判断与疾病维表中的有效性是否一致,不一样则提示
			if (diseaseAndDisable.get(tempStd) != null) {
				if (!StringUtils.equals(diseaseAndDisable.get(tempStd), disable)) {
					if (warn1.length() == 0) {
						warn1.append("该疾病名的disable填写与疾病维表不一致：" + std + ";");
					} else {
						warn1.append(std + ";");
					}
				}
				// 变成疾病维表的disable
				disable = diseaseAndDisable.get(tempStd);
			}

			// 判断有效性是否唯一
			if (disableMap.get(tempStd) != null) {
				if (!StringUtils.equals(disableMap.get(tempStd), disable)) {
					if (errMsg5.length() == 0) {
						errMsg5.append("该疾病名的disable填写不一致：" + std + ";");
					} else {
						errMsg5.append(std + ";");
					}
				}
			} else {
				disableMap.put(tempStd, disable);
			}

			// 获取疾病维表中疾病对应的一级,判断是否一致如果不一样需要提示
			if (diseaseAndCategory.get(tempStd) != null) {
				if (!StringUtils.equals(diseaseAndCategory.get(tempStd), category)) {
					if (warn2.length() == 0) {
						warn2.append("该疾病名的一级填写与疾病维表不一致：" + std + ";");
					} else {
						warn2.append(std + ";");
					}
				}
				// 变成疾病维表的category
				category = diseaseAndCategory.get(tempStd);
			}

			// 校验是否疾病名对应的一级一致
			if (categoryMap.get(tempStd) != null) {
				if (!StringUtils.equals(categoryMap.get(tempStd), category)) {
					if (errMsg6.length() == 0) {
						errMsg6.append("该疾病名的一级填写不一致：" + std + ";");
					} else {
						errMsg6.append(std + ";");
					}
				}
			} else {
				categoryMap.put(tempStd, category);
			}

			// 判断一级
			if (StringUtils.isNotBlank(category)) {
				if (!categoryDisease.contains(category)) {
					// 一级不在疾病维表中,必须在外部疾病中,且为有效疾病
					categoryList.add(category);
				}
			}

			if ("0".equals(disable)) {
				categoryDiseaseList.add(tempStd);
			}

			// 构建对象
			Map<String, String> value = new HashMap<>();
			value.put("disease_name", diseaseName);
			value.put("std", tempStd);
			value.put("disable", disable);
			value.put("category", category);
			valueMap.put(diseaseName, value);
		}

		// 校验一级
		if (categoryList != null && categoryList.size() > 0) {
			categoryList.removeAll(categoryDiseaseList);
			if (categoryList != null && categoryList.size() > 0) {
				errMsg7.append("该疾病名的一级不在标准疾病名中：" + categoryList.toString());
			}
		}
		// 校验失败
		if (errMsg.length() > 0 || errMsg2.length() > 0 || errMsg3.length() > 0 || errMsg4.length() > 0
				|| errMsg5.length() > 0 || errMsg6.length() > 0 || errMsg7.length() > 0) {
			LOGGER.info(errMsg.toString() + "\n" + errMsg2.toString() + "\n" + errMsg3.toString() + "\n"
					+ errMsg4.toString() + "\n" + errMsg5.toString() + "\n" + errMsg6.toString() + "\n"
					+ errMsg7.toString() + "\n");
			throw new RuntimeException(errMsg.toString() + "\n" + errMsg2.toString() + "\n" + errMsg3.toString() + "\n"
					+ errMsg4.toString() + "\n" + errMsg5.toString() + "\n" + errMsg6.toString() + "\n"
					+ errMsg7.toString() + "\n");
		}
		if (warn1.length() > 0 || warn2.length() > 0) {
			LOGGER.info(warn1.toString() + "\n" + warn2.toString());
			return ReturnUtil.success(warn1.toString() + "\n" + warn2.toString());

		}
		return ReturnUtil.success("成功");
	}

	private String getCellNum(Row row, int num) {
		Cell cell = row.getCell(num);
		String index = "";
		if (cell != null) {
			cell.setCellType(Cell.CELL_TYPE_STRING);
			index = cell.getStringCellValue().trim();
		}
		return index;
	}
}
